import 'dart:html';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'detail.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  ScrollController _controller = new ScrollController();
  @override
  Widget build(BuildContext context) {
    final pages = [ListViews(), ListViews()];
    int clickIndex = 0;
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('APP List'),
          centerTitle: true,
          backgroundColor: Colors.black,
          leading: IconButton(icon: Icon(Icons.list), onPressed: () {}),
          elevation: 2.0,
        ),
        bottomNavigationBar: new BottomNavigationBar(
          items: [
            BottomNavigationBarItem(
                icon: Icon(Icons.person), title: new Text('好友列表')),
            BottomNavigationBarItem(
                icon: Icon(Icons.home), title: new Text('个人中心')),
          ],
          currentIndex: clickIndex,
          type: BottomNavigationBarType.fixed,
          selectedItemColor: Colors.black,
          unselectedItemColor: Colors.black26,
          onTap: (i) {
            print(i);
          },
        ),
        body: pages[clickIndex],
      ),
    );
  }
}

/*
  无限加载列表
 */
class ListViews extends StatefulWidget {
  @override
  _ListViewsState createState() => _ListViewsState();
}

class _ListViewsState extends State<ListViews> {
  static const loading = 'end'; //结尾标记
  var _content = <String>[loading];
  @override
  Widget build(BuildContext context) {
    Image image;
    image = Image.network(
      'https://avatars2.githubusercontent.com/u/20411648?s=460&v=4',
      width: 50.0,
      height: 50.0,
    );
    return ListView.separated(
        itemBuilder: (context, index) {
          //判断是否到了末尾  最多加载20条
          if (_content[index] == loading) {
            if (_content.length - 1 < 20) {
              //获取数据
              _getMorelist();
              return Container(
                alignment: Alignment.center,
                padding: EdgeInsets.all(18.0),
                child: SizedBox(
                  width: 20.0,
                  height: 20.0,
                  child: CircularProgressIndicator(
                    strokeWidth: 1.0,
                  ),
                ),
              );
            } else {
              //  已经加载了20条  结束
              return Container(
                alignment: Alignment.center,
                padding: EdgeInsets.all(15.0),
                child: Text(
                  '没有更多了',
                  style: TextStyle(color: Colors.black),
                ),
              );
            }
          }
          //  显示列表内容
          return ListTile(
            tileColor: Colors.white,
            leading: image,
            title: Text(_content[index]),
            subtitle: Text(
              'hello ${_content[index]}',
              style: TextStyle(color: Colors.black26),
            ),
            trailing: Icon(
              Icons.keyboard_arrow_right,
              color: Colors.black26,
              size: 20.0,
            ),
            onTap: () {
              //  点击查看详情
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (builder) =>
                          ListDetails(name: _content[index])));
            },
          );
        },
        separatorBuilder: (context, index) => Divider(
              height: .1,
              color: Colors.black12,
              indent: 75,
            ),
        itemCount: _content.length);
  }

  void _getMorelist() {
    Future.delayed(Duration(seconds: 2)).then((e) {
      setState(() {
        //  重新构建列表
        _content.insertAll(_content.length - 1, [
          "Tony",
          "Jack",
          "Tony",
          "Jack",
          "Tony",
        ]);
      });
    });
  }
}
